package com.ys.service.sys.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ys.config.MyConfig;
import com.ys.entity.sys.TsFilesInfo;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.sys.TsFilesInfoMapper;
import com.ys.service.sys.TsFilesInfoService;
import com.ys.util.DateUtil;

@Service("tsFilesInfoService")
public class TsFilesInfoServiceImpl implements TsFilesInfoService {

	@Autowired
	private DiySqlMapper diySqlMapper;
	@Autowired
	private TsFilesInfoMapper tsFilesInfoMapper;
	
	@Override
	public void add(TsFilesInfo entity) {
		
		tsFilesInfoMapper.insert(entity);
	}

	@Override
	public void backupDataBase() {
		Date now = new Date();
		String time = DateUtil.getYYYY_MM_DD_HH_mm_ss(now)
				.replace("-", "")
				.replace(":", "")
				.replace(" ", "_")
				.substring(0, 11);
		String pathFolder = MyConfig.getPathDatabaseBackup();
		{
			String dataBaseName = "kjecp";
			String pathFile = pathFolder + "\\" + dataBaseName + time +".bak";
			diySqlMapper.backupDataBase(dataBaseName, pathFile);
		}
		{
			String dataBaseName = "kjecp_advert";
			String pathFile = pathFolder + "\\" + dataBaseName + time +".bak";
			diySqlMapper.backupDataBase(dataBaseName, pathFile);
		}
		{
			String dataBaseName = "kjecp_amazonorder";
			String pathFile = pathFolder + "\\" + dataBaseName + time +".bak";
			diySqlMapper.backupDataBase(dataBaseName, pathFile);
		}
		{
			String dataBaseName = "kjecp_amazonproduct";
			String pathFile = pathFolder + "\\" + dataBaseName + time +".bak";
			diySqlMapper.backupDataBase(dataBaseName, pathFile);
		}
		{
			String dataBaseName = "kjecp_finance";
			String pathFile = pathFolder + "\\" + dataBaseName + time +".bak";
			diySqlMapper.backupDataBase(dataBaseName, pathFile);
		}
		{
			String dataBaseName = "kjecp_report";
			String pathFile = pathFolder + "\\" + dataBaseName + time +".bak";
			diySqlMapper.backupDataBase(dataBaseName, pathFile);
		}
		
		// 删除更早前的备份文件
		diySqlMapper.deleteBackupDataBaseFile();
	}
}
